package day4;

import java.lang.reflect.Field;
import java.util.Date;

//获取类中的资源
public class Person extends Animal {
    public Person(Integer id) {
        this.id = id;
        System.out.println("运行有参构造");
    }
    public Person(){
        System.out.println("运行无参构造");
    }

    private Integer id;
    String name;
    protected double money;
    public Date birthday;

    public void test1() {
        System.out.println("运行test1");
    }

    public void test2(String name, Integer num) {
        System.out.println("运行test2:" + name + " " + num);
    }

    public int test3(int a, int b) {
        System.out.println("运行test3:" + a + " " + b);
        return a + b;
    }

    public static void main(String[] args) {
        //1.获取类对象
        Person p = new Person(10);
        Class<?> c = p.getClass();

        //2.通过类对象 获取类的资源
        String name = c.getName();
        System.out.println("全类名：" + name);
        String simpleName = c.getSimpleName();
        System.out.println("类名：" + simpleName);
        String superName = c.getSuperclass().getSuperclass().getName();
        System.out.println("超类：" + superName);
        System.out.println("--------------------获取属性Field--------------------");
        //可以获取类中的所有属性，但是不包括父类
        Field[] fs = c.getDeclaredFields();
        for (Field f : fs) {
            //f就是类中的每个属性
            String fieldName = f.getName();
            Class type = f.getType();
            System.out.println("属性名：" + fieldName + "，类型:" + type);
        }
        System.out.println("--------------------");
        //只能获取类中的公开资源(自己的和父类的...)
        Field[] fs2 = c.getFields();
        for (Field f : fs2) {
            //f就是类中的每个属性
            String fieldName = f.getName();
            Class type = f.getType();
            System.out.println("属性名：" + fieldName + "，类型:" + type);
        }
    }
}


class Animal {
    private int height;
    int age;
    protected String sex;
    public String address;
}
